{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from sklearn.datasets import make_classification\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = make_classification(n_samples=10000, n_features=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "kf = KFold(n_splits=5,random_state=42,shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy: 0.92\n",
      "accuracy: 0.92\n",
      "accuracy: 0.91\n",
      "accuracy: 0.92\n",
      "accuracy: 0.91\n",
      "average accuracy (over all folds): 0.92\n"
     ]
    }
   ],
   "source": [
    "accuracies = []\n",
    "\n",
    "for train_index, test_index in kf.split(X):\n",
    "\n",
    "    data_train   = X[train_index]\n",
    "    target_train = y[train_index]\n",
    "\n",
    "    data_test    = X[test_index]\n",
    "    target_test  = y[test_index]\n",
    "\n",
    "    # if needed, do preprocessing here\n",
    "\n",
    "    clf = LogisticRegression()\n",
    "    clf.fit(data_train,target_train)\n",
    "    \n",
    "    preds = clf.predict(data_test)\n",
    "        \n",
    "    accuracy = accuracy_score(target_test,preds)\n",
    "    \n",
    "    print('accuracy: {:.2f}'.format(accuracy))\n",
    "    \n",
    "    accuracies.append(accuracy)\n",
    "\n",
    "average_accuracy = np.mean(accuracies)\n",
    "print('average accuracy (over all folds): {:.2f}'.format(average_accuracy))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rlac-momento",
   "language": "python",
   "name": "rlac-momento"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
